/*
============================================================================
Name        : myJpegAppUi.cpp
Author      : 
Version     :
Copyright   : Your copyright notice
Description : Main application UI class (controller)
============================================================================
*/

// INCLUDE FILES
#include <avkon.hrh>
#include <aknnotewrappers.h>
#include <stringloader.h>
#include <myJpeg.rsg>
#include <f32file.h>
#include <s32file.h>

#include "myJpeg.pan"
#include "myJpegAppUi.h"
#include "myJpegAppView.h"
#include "myJpeg.hrh"

#include "jpeg.h"

int gMemoryCount = 0 ;

// ============================ MEMBER FUNCTIONS ===============================


// -----------------------------------------------------------------------------
// CmyJpegAppUi::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CmyJpegAppUi::ConstructL()
{
    // Initialise app UI with standard value.
    BaseConstructL();

    // Create view object
    iAppView = CmyJpegAppView::NewL( ClientRect() );
}
// -----------------------------------------------------------------------------
// CmyJpegAppUi::CmyJpegAppUi()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CmyJpegAppUi::CmyJpegAppUi()
{
    // No implementation required
}

// -----------------------------------------------------------------------------
// CmyJpegAppUi::~CmyJpegAppUi()
// Destructor.
// -----------------------------------------------------------------------------
//
CmyJpegAppUi::~CmyJpegAppUi()
{
    if ( iAppView )
    {
        delete iAppView;
        iAppView = NULL;
    }

}

// -----------------------------------------------------------------------------
// CmyJpegAppUi::HandleCommandL()
// Takes care of command handling.
// -----------------------------------------------------------------------------
//

static void _doTest()
{
    TTime startTime ; startTime.HomeTime() ;

    Picture pic;
    Picture pic2;

    pic.open("c:\\data\\test.jpg") ;

    int scale = 100;
    pic2.setsize(pic.getwidth()*scale/100, pic.getheight()*scale/100);

    for (int y=0;y<pic2.getheight();y++) {
    for (int x=0;x<pic2.getwidth();x++) {
            int xa,ya;
            xa=x*100/scale; ya=y*100/scale;
            if (xa>pic.getwidth()) 
                xa=pic.getwidth();
            if (ya>pic.getheight()) 
                ya=pic.getheight();
            pic2.setpixel(x,y,pic.getpixel(xa,ya));
        }
    }

    pic2.save("c:\\data\\dump.jpg",50,2,2);


    TTime endTime ; endTime.HomeTime() ;

    TTimeIntervalSeconds seconds ;
    endTime.SecondsFrom(startTime, seconds) ;

    TBuf<100> sStr ;
    sStr.Copy(_L("Use time ")) ;
    sStr.AppendNum(seconds.Int()) ;
    sStr.Append(_L(", memory ")) ;
    sStr.AppendNum(gMemoryCount / 1024) ;

    // Show the information Note with
    // textResource loaded with StringLoader.
    CAknInformationNote* informationNote = new ( ELeave ) CAknInformationNote(ETrue);
    informationNote->ExecuteLD( sStr );
}


extern void idct(int (*a)[8], int (*b)[8]) ;
void dct(int (*a)[8], int (*b)[8])  ;

void CmyJpegAppUi::HandleCommandL( TInt aCommand )
{
    switch( aCommand )
    {
    case EEikCmdExit:
    case EAknSoftkeyExit:
        Exit();
        break;
    case EmyJpegCommand1:
        {
            TRAPD(err, _doTest()) ;

            if (err != KErrNone) 
            {
                TBuf<80> memStr ;
                memStr.Copy(_L("_doTest error = ")) ;
                memStr.AppendNum(err) ;
                memStr.Append(_L(", memory = ")) ;
                memStr.AppendNum(gMemoryCount / 1024) ;
                CAknInformationNote* errNote = new ( ELeave ) CAknInformationNote(ETrue);
                errNote->ExecuteLD( memStr );
            }
            break; 
        }

    case EmyJpegCommand2:
        {
            const TInt readTime = 500 * 500 ;
            TTime startTime ; startTime.HomeTime() ;

            RFile file ;
            file.Open(CEikonEnv::Static()->FsSession(), _L("c:\\data\\big.jpg"), EFileRead) ;
            
            TInt size = 0 ;
            file.Size(size) ;
            TInt step = size / readTime ;

            int pos = 0 ;
            TBuf8<3> tmp ;

            for (TInt i = 0; i < readTime; i++)
            {
                file.Seek(ESeekStart, pos) ;
                file.Read(tmp) ;
                pos += step ;
            }
            
            file.Close() ;

            TTime endTime ; endTime.HomeTime() ;

            TTimeIntervalSeconds seconds ;
            endTime.SecondsFrom(startTime, seconds) ;

            TBuf<100> sStr ;
            sStr.Copy(_L("File read test Use time ")) ;
            sStr.AppendNum(seconds.Int()) ;

            // Show the information Note with
            // textResource loaded with StringLoader.
            CAknInformationNote* informationNote = new ( ELeave ) CAknInformationNote(ETrue);
            informationNote->ExecuteLD( sStr );
        }
        break;

    case EmyJpegCommand3:
        {
            int ret = 0 ;

            int ori[8][8] = {
                0, 17, 222, 65535,1000, 9999, 1111, 30,
                    0,0,0,0,0,0,0,0,
                    1, 2, 3, 4, 5 , 6 , 7, 8,
                    111, 222, 333, 444, 555, 666,777,888,
                    65535,65535,65535,65535,65535,65535,65535,65535,
                    876,989,8766,7867,122,1222,32424,45345,
                    567,5677,35434,2434,23478,9982,989,532,
                    8767,2356,123,12321,984,276,8768,23276
            } ;

            int dctData[8][8] = {0};
            int idctData[8][8] = {0};

            int i = 0 ;
            int j = 0 ;


            dct(ori, dctData) ;
            {
                RFile file ;
                file.Replace(CEikonEnv::Static()->FsSession(), _L("c:\\data\\dct.c"), EFileRead | EFileWrite) ;

                TBuf8<20> tmp ;

                int* tmpData = &(dctData[0][0]) ;

                file.Write(_L8("static const int _dctVri[8][8] = {")) ;

                for (int i=0;i<64;i++) {
                    tmp.Zero() ;
                    tmp.AppendNum(*tmpData) ;
                    if (i != 64 -1) 
                        tmp.Append(_L8(", ")) ;

                    if (i % 8 == 0) 
                        file.Write(_L8("\r\n    ")) ;

                    file.Write(tmp) ;
                    tmpData ++ ;
                }

                file.Write(_L8("\r\n} ;")) ;

                file.Close() ;
            }

            idct(dctData, idctData) ;
            {
                RFile file ;
                file.Replace(CEikonEnv::Static()->FsSession(), _L("c:\\data\\idct.c"), EFileRead | EFileWrite) ;

                TBuf8<20> tmp ;

                int* tmpData = &(idctData[0][0]) ;

                file.Write(_L8("static const int _idctVri[8][8] = {")) ;

                for (int i=0;i<64;i++) {
                    tmp.Zero() ;
                    tmp.AppendNum(*tmpData) ;
                    if (i != 64 -1) 
                        tmp.Append(_L8(", ")) ;

                    if (i % 8 == 0) 
                        file.Write(_L8("\r\n    ")) ;

                    file.Write(tmp) ;
                    tmpData ++ ;
                }

                file.Write(_L8("\r\n} ;")) ;

                file.Close() ;
            }

            for (; i < 8; i++) {
                for (j = 0; j < 8; j++) {
                    if ((ori[i][j] - idctData[i][j]) > 2) {
                        ret = -1 ;
                    }
                }
            }

            ret += 1 ;
        }    
        break;
    
    default:
        Panic( EmyJpegUi );
        break;
    }
}
// -----------------------------------------------------------------------------
//  Called by the framework when the application status pane
//  size is changed.  Passes the new client rectangle to the
//  AppView
// -----------------------------------------------------------------------------
//
void CmyJpegAppUi::HandleStatusPaneSizeChange()
{
    iAppView->SetRect( ClientRect() );

} 


// End of File
